package com.aqielife.demo.entity;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.voodoodyne.jackson.jsog.JSOGGenerator;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Property;


import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.HashSet;
import java.util.Set;

/**
 * @author aqie
 * @date 2022/02/05 21:38
 * @desc
 */
@NoArgsConstructor
@Data
@JsonIdentityInfo(generator= JSOGGenerator.class)
@NodeEntity(label = "User")
public class UserNode {
  @Id
  @GeneratedValue
  private Long nodeId;

  @Property(name = "name")
  private String name;

  @Min(value = 1, message = "Age should not be less than 1")
  @Max(value = 150, message = "Age should not be greater than 150")
  @Property(name = "age")
  private int age;

  @Property(name = "sex")
  private String sex;

  /**
   addKnows方法，只有在节点未被添加之前，调用该方法添加关系，然后再调用UserService中的create方法，
   这样关系才会同时被添加到图数据库中。如果想实现两个已有节点之间增加关系，需要单独实现，
   即controller中的addKnows方法
   */
  @Relationship(type = "know" ,direction = Relationship.OUTGOING)
  private Set<UserNode> knows = new HashSet<>();

  public Boolean addKnows(UserNode to){
    return this.knows.add(to);
  }
}
